package com.course.business.controller.admin;

import com.course.business.controller.BaseController;
import com.course.model.dto.CategoryDto;
import com.course.model.dto.PageDto;
import com.course.common.response.BaseResponse;
import com.course.model.util.ValidatorUtil;
import com.course.model.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/category")
public class CategoryController extends BaseController{

    public static final String BUSINESS_NAME = "分类";

    @Resource
    private CategoryService categoryService;

    /**
     * 列表查询
     */
    @PostMapping("/getCategoryList")
    public BaseResponse list() {
        BaseResponse response = new BaseResponse().sucess();
        response.setData(categoryService.all());
        return response;
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public BaseResponse save(@RequestBody CategoryDto categoryDto) {
        // 保存校验
        ValidatorUtil.require(categoryDto.getParent(), "父id");
        ValidatorUtil.require(categoryDto.getName(), "名称");
        ValidatorUtil.length(categoryDto.getName(), "名称", 1, 50);

        BaseResponse response = new BaseResponse().sucess();
        categoryService.save(categoryDto);
        response.setData(categoryDto);
        return response;
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public BaseResponse delete(@PathVariable String id) {
        BaseResponse response = new BaseResponse().sucess();
        categoryService.delete(id);
        return response;
    }
}
